package com.inspur.ourhis_afterend.Controller;

import com.inspur.ourhis_afterend.Dao.DrugsDao;
import com.inspur.ourhis_afterend.Entity.Dept;
import com.inspur.ourhis_afterend.Entity.Drugs;
import com.inspur.ourhis_afterend.Service.DrugsService;
import com.inspur.ourhis_afterend.Tools.PageAction;
import com.inspur.ourhis_afterend.Tools.Result;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/drug")
public class DrugsController {

    @Autowired
    DrugsService drugsService;

    @Autowired
    DrugsDao drugsDao;


    @ApiOperation("查询全部的药品")
    @ApiParam("传入分页")
    @PostMapping("/findAll")
    public Result findAll(@RequestBody PageAction pageAction) {
        try {
            return Result.success(drugsService.findAll(pageAction));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failure(e.toString());
        }
    }

    @ApiParam("传入查询条件")
    @ApiOperation("药品查询")
    @PostMapping("/search")
    public Result search(@RequestParam("drugsid") String drugsid,
                         @RequestParam("name") String name) {
        try {
            return Result.success(drugsService.search(drugsid, name));
        } catch (Exception e) {
            e.printStackTrace();
            return     Result.failure(e.toString());
        }
    }

    @ApiOperation("添加药品")
    @PostMapping("/add")
    @ApiParam(value = "传入一个药品对象")
    public Result add(@RequestBody Drugs drugs) {
        System.out.println(drugs);
        try {
            drugsService.add(drugs);
            return Result.success("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failure(e.toString());
        }
    }

    @ApiOperation("修改药品")
    @PostMapping("/edit")
    @ApiParam(value = "传入一个药品对象")
    public Result edit(@RequestBody Drugs drugs,
                       @Param("drugsid") String drugsid) {
        System.out.println(drugsid);
        drugs.setDrugsid(drugsid);
        try {
            drugsService.edit(drugs);
            return Result.success("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failure(e.toString());
        }
    }


    @ApiOperation("删除药品")
    @PostMapping("/del")
    @ApiParam(value = "传入一个药品id")
    public Result del(@Param("drugsid") String drugsid) {
        try {
            int flag = drugsService.del(drugsid);
            if (flag > 0) {
                return Result.success("删除成功");
            } else {
                return Result.success("删除失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failure(e.toString());
        }
    }



    @ApiOperation("修改药品")
    @PostMapping("/setNum")
    @ApiParam(value = "传入一个药品对象")
    public Result setNum(@Param("num") Integer num,
                       @Param("drugsid") String drugsid) {
        System.out.println(drugsid);
        try {
            Drugs drugs = drugsDao.selectById(drugsid);
            drugs.setNum(num);
            drugsDao.updateById(drugs);
            return Result.success("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failure(e.toString());
        }
    }
}
